package at.fhj.itm10.ticketmaster.helper;

import org.apache.log4j.Logger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Ballach md5 hashmaker from emailaddress called by mailservive, insert
 *         md5 in registerbean
 */
public class MD5HashmakerEmail {

	private static String md5code;
	private static Logger logger = Logger.getLogger("at.fhj.itm10.ticketmaster.helper.MD5HashmakerEmail");
	
	public static String generateMD5Code(String recipient)
			throws NoSuchAlgorithmException {

		logger.debug("MD5HashmakerMail");
		logger.info(recipient);
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(recipient.getBytes());

		byte byteData[] = md.digest();

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < byteData.length; i++) {
			sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
					.substring(1));

		}
		
		logger.info("MD5="+sb.toString());
		//System.out.println("MD5 : " + sb.toString());
		md5code = sb.toString();
		logger.info("MD5Code="+md5code);
		//System.out.println(md5code);
		return md5code;

	}

	public static String getMd5code() {
		return md5code;
	}

	public static void setMd5code(String md5code) {
		MD5HashmakerEmail.md5code = md5code;
	}

}
